clear all
set more off
set maxvar 10000


	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    
    gen female = (sex==2)
    tab female, m
    label var female "Female"

    tempfile alldata
    save `alldata'

    foreach x in all white black {

        use `alldata', clear

        if "`x'"=="all" {
            local sample ""
            local yscale "-.1(.05)0.1"
        }
        if "`x'"=="white" {
            local sample "keep if race==1"
            local yscale "-.1(.05)0.1"
        }
        if "`x'"=="black" {
            local sample "keep if race==2"
            local yscale "-.25(.1)0.25"
        }

        `sample'
        
        forval i=1(1)2 {
            gen est_`i'=. 
            gen est_lb_`i' =.
            gen est_ub_`i' =.
        }
        
        levelsof decade, local(decades)
            foreach d of local decades {    
                di "`d'"
                reg log_father_baseline female i.survey_year [aw=wgt_sex_race] if decade==`d', robust 
                replace est_1 = _b[female] if decade==`d'
                replace est_ub_1 = _b[female]+1.96*_se[female] if decade==`d'
                replace est_lb_1 = _b[female]-1.96*_se[female] if decade==`d'
            }
                
            foreach d of local decades {
                di "`d'"
                reg rank_father_baseline female i.survey_year [aw=wgt_sex_race] if decade==`d', robust 
                replace est_2 = _b[female] if decade==`d'
                replace est_ub_2 = _b[female]+1.96*_se[female] if decade==`d'
                replace est_lb_2 = _b[female]-1.96*_se[female] if decade==`d'       
            }

        bysort decade: keep if _n==1
        keep decade est_*
        
        reshape long est_ est_lb_ est_ub_, i(decade) j(estimate)
        replace decade= decade+2 if estimate==2

    * Figures
        #delimit ;
            twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(emerald) msize(medium) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(emerald) yaxis(1) lwidth(0.5)),
            yline(0,lpat(dash_dot) lcolor(gs9) lwidth(medthick))
            xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
            ylabel(`yscale', axis(1)) yti("Coefficient on female" " ", axis(1)) legend(off)
            xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
        #delimit cr
        graph export "$Mydirectory2/appendix_c/corr_fatherinc_sex_bydecade_log_series_`x'.pdf", as(pdf) replace

        #delimit ;
            twoway (scatter est_ decade if estimate==2,  msymbol(diamond) mcolor(gold*1.25) msize(small) yaxis(1)) 
                   (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(gold*1.25) yaxis(1) lwidth(0.5)),
            xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
            yline(0,lpat(dash_dot) lcolor(gs9) lwidth(medthick))
            ylabel(-8(2)8, axis(1)) yti("Coefficient on female" " ", axis(1)) legend(off)
            xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
        #delimit cr
        graph export "$Mydirectory2/appendix_c/corr_fatherinc_sex_bydecade_rank_series_`x'.pdf", as(pdf) replace

        drop est_*

    }
    